package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.GroupCompany;
import com.ruoyi.system.mapper.EmployeeBasicInfoMapper;
import com.ruoyi.system.domain.EmployeeBasicInfo;
import com.ruoyi.system.service.IEmployeeBasicInfoService;

/**
 * 员工基本信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-22
 */
@Service
public class EmployeeBasicInfoServiceImpl implements IEmployeeBasicInfoService 
{
    @Autowired
    private EmployeeBasicInfoMapper employeeBasicInfoMapper;

    /**
     * 查询员工基本信息
     * 
     * @param employeeId 员工基本信息主键
     * @return 员工基本信息
     */
    @Override
    public EmployeeBasicInfo selectEmployeeBasicInfoByEmployeeId(String employeeId)
    {
        return employeeBasicInfoMapper.selectEmployeeBasicInfoByEmployeeId(employeeId);
    }

    /**
     * 查询员工基本信息列表
     * 
     * @param employeeBasicInfo 员工基本信息
     * @return 员工基本信息
     */
    @Override
    public List<EmployeeBasicInfo> selectEmployeeBasicInfoList(EmployeeBasicInfo employeeBasicInfo)
    {
        return employeeBasicInfoMapper.selectEmployeeBasicInfoList(employeeBasicInfo);
    }

    /**
     * 新增员工基本信息
     * 
     * @param employeeBasicInfo 员工基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEmployeeBasicInfo(EmployeeBasicInfo employeeBasicInfo)
    {
        employeeBasicInfo.setCreateTime(DateUtils.getNowDate());
        int rows = employeeBasicInfoMapper.insertEmployeeBasicInfo(employeeBasicInfo);
        insertGroupCompany(employeeBasicInfo);
        return rows;
    }

    /**
     * 修改员工基本信息
     * 
     * @param employeeBasicInfo 员工基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEmployeeBasicInfo(EmployeeBasicInfo employeeBasicInfo)
    {
        employeeBasicInfo.setUpdateTime(DateUtils.getNowDate());
        employeeBasicInfoMapper.deleteGroupCompanyByParentId(employeeBasicInfo.getEmployeeId());
        insertGroupCompany(employeeBasicInfo);
        return employeeBasicInfoMapper.updateEmployeeBasicInfo(employeeBasicInfo);
    }

    /**
     * 批量删除员工基本信息
     * 
     * @param employeeIds 需要删除的员工基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEmployeeBasicInfoByEmployeeIds(String[] employeeIds)
    {
        employeeBasicInfoMapper.deleteGroupCompanyByParentIds(employeeIds);
        return employeeBasicInfoMapper.deleteEmployeeBasicInfoByEmployeeIds(employeeIds);
    }

    /**
     * 删除员工基本信息信息
     * 
     * @param employeeId 员工基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEmployeeBasicInfoByEmployeeId(String employeeId)
    {
        employeeBasicInfoMapper.deleteGroupCompanyByParentId(employeeId);
        return employeeBasicInfoMapper.deleteEmployeeBasicInfoByEmployeeId(employeeId);
    }

    /**
     * 新增集团及子公司信息信息
     * 
     * @param employeeBasicInfo 员工基本信息对象
     */
    public void insertGroupCompany(EmployeeBasicInfo employeeBasicInfo)
    {
        List<GroupCompany> groupCompanyList = employeeBasicInfo.getGroupCompanyList();
        String employeeId = employeeBasicInfo.getEmployeeId();
        if (StringUtils.isNotNull(groupCompanyList))
        {
            List<GroupCompany> list = new ArrayList<GroupCompany>();
            for (GroupCompany groupCompany : groupCompanyList)
            {
                groupCompany.setParentId(Long.valueOf(employeeId));
                list.add(groupCompany);
            }
            if (list.size() > 0)
            {
                employeeBasicInfoMapper.batchGroupCompany(list);
            }
        }
    }
}
